/*
 * PageList.java 1.0 Created on 2003. 2. 24.
 */

package com.zll.framework.util;
import java.util.Date;
import java.util.Properties;
 
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.zll.framework.conf.Configuration;
	 
	 
public class EmailUtil {
	 
    protected final Log logger = LogFactory.getLog(getClass());
     
    private Properties props;
     
    public EmailUtil() {
    	props = new Properties();
//    	props.put("mail.smtp.starttls.enable", "true");
//    	props.put("mail.smtp.host", "smtp.gmail.com");
//    	props.put("mail.smtp.auth", "true");
//    	props.put("mail.smtp.port", "587");

    	props.put("mail.smtp.host", Configuration.get("smtp.host"));
    	props.put("mail.smtp.auth","true");
    }
     
    public void sendEmail(String from, String fromName, String to, String subject, String content) throws Exception {
 
        EmailAuthenticator authenticator = new EmailAuthenticator();
 
        Session session = Session.getInstance(props, authenticator);
         
        try {
            Message msg = new MimeMessage(session);
             
            msg.setFrom(new InternetAddress(from, fromName));
            logger.debug(msg.getFrom()[0].toString());
            // Recipients 받는사람
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            msg.setSubject(subject);
            msg.setContent(content, "text/html; charset=UTF-8");
            msg.setSentDate(new Date());
            Transport.send(msg);
        } catch (MessagingException e) {
 
            e.printStackTrace();
 
        }
 
    }
     
    class EmailAuthenticator extends Authenticator {
 
        private String id; 
        private String pw; 
 
        public EmailAuthenticator(){
            this.id = Configuration.get("smtp.id");	//"u2warehdm@gmail.com";
            this.pw = Configuration.get("smtp.pw");	//"Gmail비밀번호";
        }
         
        public EmailAuthenticator(String id, String pw) {
            this.id = id;
            this.pw = pw;
        }
 
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(id, pw);
        }
 
    }
	 
	
}
